home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  14.3 KB  |  497 lines

  1. /* Copyright (C) 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfcmap.c,v 1.8 2000/09/19 19:00:53 lpd Exp $ */
  20. /* CMap creation operator */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gsmatrix.h"        /* for gxfont.h */
  25. #include "gsstruct.h"
  26. #include "gsutil.h"        /* for bytes_compare */
  27. #include "gxfcmap.h"
  28. #include "gxfont.h"
  29. #include "ialloc.h"
  30. #include "icid.h"
  31. #include "iddict.h"
  32. #include "idparam.h"
  33. #include "ifont.h"        /* for zfont_mark_glyph_name */
  34. #include "iname.h"
  35. #include "store.h"
  36.  
  37. /*
  38.  * Define whether to check the compatibility of CIDSystemInfo between the
  39.  * CMap and the composite font.  PLRM2 says compatibility is required, but
  40.  * PLRM3 says the interpreter doesn't check it.
  41.  */
  42. /*#define CHECK_CID_SYSTEM_INFO_COMPATIBILITY*/
  43.  
  44. /* ---------------- Internal procedures ---------------- */
  45.  
  46. /* Free a code map in case of VMerror. */
  47. private void
  48. free_code_map(gx_code_map_t * pcmap, gs_memory_t * mem)
  49. {
  50.     if (pcmap->lookup) {
  51.     int i;
  52.  
  53.     for (i = 0; i < pcmap->num_lookup; ++i) {
  54.         gx_code_lookup_range_t *pclr = &pcmap->lookup[i];
  55.  
  56.         if (pclr->value_type == CODE_VALUE_GLYPH)
  57.         gs_free_string(mem, pclr->values.data, pclr->values.size,
  58.                    "free_code_map(values)");
  59.     }
  60.     gs_free_object(mem, pcmap->lookup, "free_code_map(map)");
  61.     }
  62. }
  63.  
  64. /* Convert code ranges to internal form. */
  65. private int
  66. acquire_code_ranges(gs_cmap_t *cmap, const ref * pref, gs_memory_t * mem)
  67. {
  68.     uint num_ranges;
  69.     gx_code_space_range_t *ranges;
  70.     uint i;
  71.  
  72.     if (!r_is_array(pref) || (num_ranges = r_size(pref)) == 0 ||
  73.     num_ranges & 1)
  74.     return_error(e_rangecheck);
  75.     num_ranges >>= 1;
  76.     ranges = (gx_code_space_range_t *)
  77.     gs_alloc_byte_array(mem, num_ranges, sizeof(gx_code_space_range_t),
  78.                 "acquire_code_ranges");
  79.     if (ranges == 0)
  80.     return_error(e_VMerror);
  81.     cmap->code_space.ranges = ranges;
  82.     cmap->code_space.num_ranges = num_ranges;
  83.     for (i = 0; i < num_ranges; ++i, ++ranges) {
  84.     ref rfirst, rlast;
  85.     int size;
  86.  
  87.     array_get(pref, i * 2L, &rfirst);
  88.     array_get(pref, i * 2L + 1, &rlast);
  89.     if (!r_has_type(&rfirst, t_string) ||
  90.         !r_has_type(&rlast, t_string) ||
  91.         (size = r_size(&rfirst)) == 0 || size > MAX_CMAP_CODE_SIZE ||
  92.         r_size(&rlast) != size ||
  93.         memcmp(rfirst.value.bytes, rlast.value.bytes, size) > 0)
  94.         return_error(e_rangecheck);
  95.     memcpy(ranges->first, rfirst.value.bytes, size);
  96.     memcpy(ranges->last, rlast.value.bytes, size);
  97.     ranges->size = size;
  98.     }
  99.     return 0;
  100. }
  101.  
  102. /* Convert a code map to internal form. */
  103. private int
  104. acquire_code_map(gx_code_map_t * pcmap, const ref * pref, gs_cmap_t * root,
  105.          gs_memory_t * mem)
  106. {
  107.     uint num_lookup;
  108.     gx_code_lookup_range_t *pclr;
  109.     long i;
  110.  
  111.     if (!r_is_array(pref) || (num_lookup = r_size(pref)) % 5 != 0)
  112.     return_error(e_rangecheck);
  113.     num_lookup /= 5;
  114.     pclr = gs_alloc_struct_array(mem, num_lookup, gx_code_lookup_range_t,
  115.                  &st_code_lookup_range_element,
  116.                  "acquire_code_map(lookup ranges)");
  117.     if (pclr == 0)
  118.     return_error(e_VMerror);
  119.     memset(pclr, 0, sizeof(*pclr) * num_lookup);
  120.     pcmap->lookup = pclr;
  121.     pcmap->num_lookup = num_lookup;
  122.     for (i = 0; i < num_lookup * 5; i += 5, ++pclr) {
  123.     ref rprefix, rmisc, rkeys, rvalues, rfxs;
  124.  
  125.     array_get(pref, i, &rprefix);
  126.     array_get(pref, i + 1, &rmisc);
  127.     array_get(pref, i + 2, &rkeys);
  128.     array_get(pref, i + 3, &rvalues);
  129.     array_get(pref, i + 4, &rfxs);
  130.  
  131.     if (!r_has_type(&rprefix, t_string) ||
  132.         !r_has_type(&rmisc, t_string) ||
  133.         !r_has_type(&rkeys, t_string) ||
  134.         !(r_has_type(&rvalues, t_string) || r_is_array(&rvalues)) ||
  135.         !r_has_type(&rfxs, t_integer)
  136.         )
  137.         return_error(e_typecheck);
  138.     if (r_size(&rmisc) != 4 ||
  139.         rmisc.value.bytes[0] > MAX_CMAP_CODE_SIZE - r_size(&rprefix) ||
  140.         rmisc.value.bytes[1] > 1 ||
  141.         rmisc.value.bytes[2] > CODE_VALUE_MAX ||
  142.         rmisc.value.bytes[3] == 0)
  143.         return_error(e_rangecheck);
  144.     pclr->cmap = root;
  145.     pclr->key_size = rmisc.value.bytes[0];
  146.     pclr->key_prefix_size = r_size(&rprefix);
  147.     memcpy(pclr->key_prefix, rprefix.value.bytes, pclr->key_prefix_size);
  148.     pclr->key_is_range = rmisc.value.bytes[1];
  149.     if (pclr->key_size == 0) {
  150.         /* This is a single entry consisting only of the prefix. */
  151.         if (r_size(&rkeys) != 0)
  152.         return_error(e_rangecheck);
  153.         pclr->num_keys = 1;
  154.     } else {
  155.         int step = pclr->key_size * (pclr->key_is_range ? 2 : 1);
  156.  
  157.         if (r_size(&rkeys) % step != 0)
  158.         return_error(e_rangecheck);
  159.         pclr->num_keys = r_size(&rkeys) / step;
  160.     }
  161.     pclr->keys.data = rkeys.value.bytes,
  162.         pclr->keys.size = r_size(&rkeys);
  163.     pclr->value_type = rmisc.value.bytes[2];
  164.     pclr->value_size = rmisc.value.bytes[3];
  165.     if (r_has_type(&rvalues, t_string)) {
  166.         if (pclr->value_type == CODE_VALUE_GLYPH)
  167.         return_error(e_rangecheck);
  168.         if (r_size(&rvalues) % pclr->num_keys != 0 ||
  169.         r_size(&rvalues) / pclr->num_keys != pclr->value_size)
  170.         return_error(e_rangecheck);
  171.         pclr->values.data = rvalues.value.bytes,
  172.         pclr->values.size = r_size(&rvalues);
  173.     } else {
  174.         uint values_size = pclr->num_keys * pclr->value_size;
  175.         long k;
  176.         byte *pvalue;
  177.  
  178.         if (pclr->value_type != CODE_VALUE_GLYPH ||
  179.         r_size(&rvalues) != pclr->num_keys ||
  180.         pclr->value_size > sizeof(gs_glyph))
  181.         return_error(e_rangecheck);
  182.         pclr->values.data = gs_alloc_string(mem, values_size,
  183.                         "acquire_code_map(values)");
  184.         if (pclr->values.data == 0)
  185.         return_error(e_VMerror);
  186.         pclr->values.size = values_size;
  187.         pvalue = pclr->values.data;
  188.         for (k = 0; k < pclr->num_keys; ++k) {
  189.         ref rvalue;
  190.         gs_glyph value;
  191.         int i;
  192.  
  193.         array_get(&rvalues, k, &rvalue);
  194.         if (!r_has_type(&rvalue, t_name))
  195.             return_error(e_rangecheck);
  196.         value = name_index(&rvalue);
  197.         /*
  198.          * We need a special check here because some CPUs cannot
  199.          * shift by the full size of an int or long.
  200.          */
  201.         if (pclr->value_size < sizeof(value) &&
  202.             (value >> (pclr->value_size * 8)) != 0
  203.             )
  204.             return_error(e_rangecheck);
  205.         for (i = pclr->value_size; --i >= 0; )
  206.             *pvalue++ = (byte)(value >> (i * 8));
  207.         }
  208.     }
  209.     check_int_leu_only(rfxs, 0xff);
  210.     pclr->font_index = (int)rfxs.value.intval;
  211.     }
  212.     return 0;
  213. }
  214.  
  215. /*
  216.  * Acquire the CIDSystemInfo array from a dictionary.  If missing, fabricate
  217.  * a 0-element array and return 1.
  218.  */
  219. private int
  220. acquire_cid_system_info(ref *psia, const ref *op)
  221. {
  222.     ref *prcidsi;
  223.  
  224.     if (dict_find_string(op, "CIDSystemInfo", &prcidsi) <= 0) {
  225.     make_empty_array(psia, a_readonly);
  226.     return 1;
  227.     }
  228.     if (r_has_type(prcidsi, t_dictionary)) {
  229.     make_array(psia, a_readonly, 1, prcidsi);
  230.     return 0;
  231.     }
  232.     if (!r_is_array(prcidsi))
  233.     return_error(e_typecheck);
  234.     *psia = *prcidsi;
  235.     return 0;
  236. }
  237.  
  238. /*
  239.  * Get one element of a CIDSystemInfo array.  If the element is missing or
  240.  * null, return 1.
  241.  */
  242. private int
  243. get_cid_system_info(gs_cid_system_info_t *pcidsi, const ref *psia, uint index)
  244. {
  245.     ref rcidsi;
  246.     int code = array_get(psia, (long)index, &rcidsi);
  247.  
  248.     if (code < 0 || r_has_type(&rcidsi, t_null)) {
  249.     cid_system_info_set_null(pcidsi);
  250.     return 1;
  251.     }
  252.     return cid_system_info_param(pcidsi, &rcidsi);
  253. }
  254.  
  255. #ifdef CHECK_CID_SYSTEM_INFO_COMPATIBILITY
  256.  
  257. /* Check compatibility of CIDSystemInfo. */
  258. private bool
  259. bytes_eq(const gs_const_string *pcs1, const gs_const_string *pcs2)
  260. {
  261.     return !bytes_compare(pcs1->data, pcs1->size,
  262.               pcs2->data, pcs2->size);
  263. }
  264. private bool
  265. cid_system_info_compatible(const gs_cid_system_info_t * psi1,
  266.                const gs_cid_system_info_t * psi2)
  267. {
  268.     return bytes_eq(&psi1->Registry, &psi2->Registry) &&
  269.     bytes_eq(&psi1->Ordering, &psi2->Ordering);
  270. }
  271.  
  272. #endif /* CHECK_CID_SYSTEM_INFO_COMPATIBILITY */
  273.  
  274. /* ---------------- (Semi-)public procedures ---------------- */
  275.  
  276. /* Get the CodeMap from a Type 0 font, and check the CIDSystemInfo of */
  277. /* its subsidiary fonts. */
  278. int
  279. ztype0_get_cmap(const gs_cmap_t **ppcmap, const ref *pfdepvector,
  280.         const ref *op, gs_memory_t *imem)
  281. {
  282.     ref *prcmap;
  283.     ref *pcodemap;
  284.     const gs_cmap_t *pcmap;
  285.     int code;
  286.     uint num_fonts;
  287.     uint i;
  288.  
  289.     if (dict_find_string(op, "CMap", &prcmap) <= 0 ||
  290.     !r_has_type(prcmap, t_dictionary) ||
  291.     dict_find_string(prcmap, "CodeMap", &pcodemap) <= 0 ||
  292.     !r_has_stype(pcodemap, imem, st_cmap)
  293.     )
  294.     return_error(e_invalidfont);
  295.     pcmap = r_ptr(pcodemap, gs_cmap_t);
  296.     num_fonts = r_size(pfdepvector);
  297.     for (i = 0; i < num_fonts; ++i) {
  298.     ref rfdep, rfsi;
  299.  
  300.     array_get(pfdepvector, (long)i, &rfdep);
  301.     code = acquire_cid_system_info(&rfsi, &rfdep);
  302.     if (code < 0)
  303.         return code;
  304.     if (code == 0) {
  305.         if (r_size(&rfsi) != 1)
  306.         return_error(e_rangecheck);
  307. #ifdef CHECK_CID_SYSTEM_INFO_COMPATIBILITY
  308.         {
  309.         gs_cid_system_info_t cidsi;
  310.  
  311.         get_cid_system_info(&cidsi, &rfsi, 0);
  312.         if (!cid_system_info_is_null(&cidsi) &&
  313.             !cid_system_info_compatible(&cidsi,
  314.                         pcmap->CIDSystemInfo + i))
  315.             return_error(e_rangecheck);
  316.         }
  317. #endif
  318.     }
  319.     }
  320.     *ppcmap = pcmap;
  321.     return 0;
  322. }
  323.  
  324. /* ---------------- Operators ---------------- */
  325.  
  326. /* <CMap> .buildcmap <CMap> */
  327. /*
  328.  * Create the internal form of a CMap.  The initial CMap must be read-write
  329.  * and have an entry with key = CodeMap and value = null; the result is
  330.  * read-only and has a real CodeMap.
  331.  *
  332.  * This operator reads the CMapType, CMapName, CIDSystemInfo, CMapVersion,
  333.  * UIDOffset, XUID, WMode, and .CodeMapData elements of the CMap dictionary.
  334.  * For details, see lib/gs_cmap.ps and the Adobe documentation.
  335.  */
  336. private int
  337. zfcmap_glyph_name(gs_glyph glyph, gs_const_string *pstr, void *proc_data)
  338. {
  339.     ref nref, nsref;
  340.     int code = 0;
  341.  
  342.     /*code = */name_index_ref((uint)glyph, &nref);
  343.     if (code < 0)
  344.     return code;
  345.     name_string_ref(&nref, &nsref);
  346.     pstr->data = nsref.value.const_bytes;
  347.     pstr->size = r_size(&nsref);
  348.     return 0;
  349. }
  350. private int
  351. zbuildcmap(i_ctx_t *i_ctx_p)
  352. {
  353.     os_ptr op = osp;
  354.     int code;
  355.     ref *pcmapname;
  356.     ref *puidoffset;
  357.     ref *pcodemapdata;
  358.     ref *pcodemap;
  359.     ref rname, rcidsi, rcoderanges, rdefs, rnotdefs;
  360.     gs_cmap_t *pcmap = 0;
  361.     gs_cid_system_info_t *pcidsi = 0;
  362.     ref rcmap;
  363.     uint i;
  364.  
  365.     check_type(*op, t_dictionary);
  366.     check_dict_write(*op);
  367.     pcmap = ialloc_struct(gs_cmap_t, &st_cmap, "zbuildcmap(cmap)");
  368.     if (pcmap == 0) {
  369.     code = gs_note_error(e_VMerror);
  370.     goto fail;
  371.     }
  372.     gs_cmap_init(pcmap);
  373.     if ((code = dict_int_param(op, "CMapType", 0, 1, -1, &pcmap->CMapType)) < 0 ||
  374.     (code = dict_float_param(op, "CMapVersion", 0.0, &pcmap->CMapVersion)) < 0 ||
  375.     (code = dict_uid_param(op, &pcmap->uid, 0, imemory, i_ctx_p)) < 0 ||
  376.     (code = dict_int_param(op, "WMode", 0, 1, 0, &pcmap->WMode)) < 0
  377.     )
  378.     goto fail;
  379.     if ((code = dict_find_string(op, "CMapName", &pcmapname)) <= 0) {
  380.     code = gs_note_error(e_rangecheck);
  381.     goto fail;
  382.     }
  383.     if (!r_has_type(pcmapname, t_name)) {
  384.     code = gs_note_error(e_typecheck);
  385.     goto fail;
  386.     }
  387.     name_string_ref(pcmapname, &rname);
  388.     pcmap->CMapName.data = rname.value.const_bytes;
  389.     pcmap->CMapName.size = r_size(&rname);
  390.     if (dict_find_string(op, "UIDOffset", &puidoffset) > 0) {
  391.     if (!r_has_type(puidoffset, t_integer)) {
  392.         code = gs_note_error(e_typecheck);
  393.         goto fail;
  394.     }
  395.     pcmap->UIDOffset = puidoffset->value.intval; /* long, not int */
  396.     }
  397.     if (dict_find_string(op, ".CodeMapData", &pcodemapdata) <= 0 ||
  398.     !r_has_type(pcodemapdata, t_array) ||
  399.     r_size(pcodemapdata) != 3 ||
  400.     dict_find_string(op, "CodeMap", &pcodemap) <= 0 ||
  401.     !r_has_type(pcodemap, t_null)
  402.     ) {
  403.     code = gs_note_error(e_rangecheck);
  404.     goto fail;
  405.     }
  406.     if ((code = acquire_cid_system_info(&rcidsi, op)) < 0)
  407.     goto fail;
  408.     pcidsi = ialloc_struct_array(r_size(&rcidsi), gs_cid_system_info_t,
  409.                  &st_cid_system_info_element,
  410.                  "zbuildcmap(CIDSystemInfo)");
  411.     if (pcidsi == 0) {
  412.     code = gs_note_error(e_VMerror);
  413.     goto fail;
  414.     }
  415.     pcmap->CIDSystemInfo = pcidsi;
  416.     pcmap->num_fonts = r_size(&rcidsi);
  417.     for (i = 0; i < r_size(&rcidsi); ++i) {
  418.     code = get_cid_system_info(pcidsi + i, &rcidsi, i);
  419.     if (code < 0)
  420.         goto fail;
  421.     }
  422.     array_get(pcodemapdata, 0L, &rcoderanges);
  423.     array_get(pcodemapdata, 1L, &rdefs);
  424.     array_get(pcodemapdata, 2L, &rnotdefs);
  425.     if ((code = acquire_code_ranges(pcmap, &rcoderanges, imemory)) < 0)
  426.     goto fail;
  427.     if ((code = acquire_code_map(&pcmap->def, &rdefs, pcmap, imemory)) < 0)
  428.     goto fail;
  429.     if ((code = acquire_code_map(&pcmap->notdef, &rnotdefs, pcmap, imemory)) < 0)
  430.     goto fail;
  431.     pcmap->mark_glyph = zfont_mark_glyph_name;
  432.     pcmap->mark_glyph_data = 0;
  433.     pcmap->glyph_name = zfcmap_glyph_name;
  434.     pcmap->glyph_name_data = 0;
  435.     make_istruct_new(&rcmap, a_readonly, pcmap);
  436.     code = idict_put_string(op, "CodeMap", &rcmap);
  437.     if (code < 0)
  438.     goto fail;
  439.     return zreadonly(i_ctx_p);
  440. fail:
  441.     free_code_map(&pcmap->notdef, imemory);
  442.     free_code_map(&pcmap->def, imemory);
  443.     ifree_object(pcmap, "zbuildcmap(cmap)");
  444.     ifree_object(pcidsi, "zbuildcmap(CIDSystemInfo)");
  445.     return code;
  446. }
  447.  
  448. #if defined(DEBUG) || defined(PROFILE) || defined(TEST)
  449.  
  450. #include "stream.h"
  451. #include "spprint.h"
  452. #include "files.h"
  453. #include "gdevpsf.h"
  454.  
  455. /* <file> <cmap> .writecmap - */
  456. private int
  457. zfcmap_put_name_default(stream *s, const byte *str, uint size)
  458. {
  459.     pputc(s, '/');
  460.     pwrite(s, str, size);
  461.     return 0;
  462. }
  463. private int
  464. zwritecmap(i_ctx_t *i_ctx_p)
  465. {
  466.     os_ptr op = osp;
  467.     ref *pcodemap;
  468.     gs_cmap_t *pcmap;
  469.     int code;
  470.     stream *s;
  471.  
  472.     check_type(*op, t_dictionary);
  473.     if (dict_find_string(op, "CodeMap", &pcodemap) <= 0 ||
  474.     !r_is_struct(pcodemap)
  475.     )
  476.     return_error(e_typecheck);
  477.     check_write_file(s, op - 1);
  478.     pcmap = r_ptr(pcodemap, gs_cmap_t);
  479.     code = psf_write_cmap(s, pcmap, zfcmap_put_name_default, NULL);
  480.     if (code >= 0)
  481.     pop(2);
  482.     return code;
  483. }
  484.  
  485. #endif
  486.  
  487. /* ------ Initialization procedure ------ */
  488.  
  489. const op_def zfcmap_op_defs[] =
  490. {
  491.     {"1.buildcmap", zbuildcmap},
  492. #if defined(DEBUG) || defined(PROFILE) || defined(TEST)
  493.     {"2.writecmap", zwritecmap},
  494. #endif
  495.     op_def_end(0)
  496. };
  497.